package com.example.csupermaket.controller;


import com.example.csupermaket.pojo.dto.ProductAddNewDTO;
import com.example.csupermaket.pojo.dto.ProductUpdateDTO;
import com.example.csupermaket.pojo.vo.ProductListItemVO;
import com.example.csupermaket.pojo.vo.ProductStandardVO;
import com.example.csupermaket.service.IProductService;
import com.example.csupermaket.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.util.List;


@RestController
@RequestMapping("/products")
@Validated
@Api(tags = "01.商品管理模块")
public class ProductController {

    @Autowired
    private IProductService productService;

    @PostMapping("/add-new")
    @ApiOperation("01.添加商品")
    @ApiOperationSupport(order = 100)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "price", value = "商品价格", required = true, dataType = "Double"),
            @ApiImplicitParam(name = "inventory", value = "库存", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "sort", value = "排序优先级", required = true, dataType = "Integer")
    })
    public JsonResult addNew(@Valid ProductAddNewDTO productAddNewDTO){

         productService.addNew(productAddNewDTO);

        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("02.删除商品")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name="id",value = "商品id",required = true, dataType ="int")
    })
    public JsonResult delete(@Range(min = 1, message = "删除商品失败，ID值无效！")
                                 @PathVariable int id){
        productService.delete(id);
        return JsonResult.ok();

    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("03.修改商品")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name="id",value = "相册id",required = true, dataType ="int")
    public JsonResult update(@Range(min = 1, message = "删除相册失败，ID值无效！")
                           @PathVariable int id,@Valid ProductUpdateDTO productUpdateDTO){
        productService.update(id,productUpdateDTO);
        return JsonResult.ok();

    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询商品详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "获取商品详情失败，ID值无效！")
                                      @PathVariable int id) {
        ProductStandardVO queryResult = productService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("")
    @ApiOperation("04.查询商品列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list(){
        List<ProductListItemVO> productList = productService.list();
        return JsonResult.ok(productList);
    }



}
